<?php
class HomeController extends AppController {
	public $uses = array('Users');
	
	public function index() {
		$this->loadModel("User");
		$userId = $this->Auth->user('id');
		if (isset($userId)){
			$userName = $this->User->find('first', array(
				'fields' => 'display_name',
				'conditions' => array(
					'id' => $userId
				)
			));
		}
		if ($this->request->is('post')) {
			$postData = $this->request->data;
			
			if ($postData['new_password'] == $postData['confirm_new_password']) {
				$userInfo = $this->User->find('first', array(
					'conditions' => array(
						'id' => $userId,
						),
					));
				if ($userInfo['User']['password'] == md5($postData['password'])) {
					$this->User->id = $userId;
					$this->User->saveField('password', md5($postData['new_password']));
					
					$this->Session->setFlash('Save successfully !', 'default', array('class' => 'success'));
					$this->redirect(array('controller' => 'home', 'action' => 'index'));
				} else {
					$this->Session->setFlash('Current password is not valid !!!');
				}
			} else {
				$this->Session->setFlash('Recheck new passowrd !!!');
			}
		}
		$this->set('userName', $userName);
		$this->set('title_for_content', 'Profile');
		$this->set('active_main_menu_id', 0);
		$this->set('active_sub_menu_id', 0);
	}
}